﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TrainingsApp.WebModel;
using TrainingsApp.Model;
using TrainingsApp.Logic;
using ZJFramework10.Extension;
namespace TrainingsApp.IPADApp.UserControls.Main.Teacher {
    public partial class UC_Preview_Group : AccountControl {

        TS_Preview_Entity m_PreviewModel;
        protected TS_Preview_Entity PreviewModel {
            get {
                return m_PreviewModel;
            }
            set {


                m_PreviewModel = value;
            }
        }

        TS_Question_Entity m_QuestionModel;
        protected TS_Question_Entity QuestionModel {
            get {
                return m_QuestionModel;
            }
            set {
                m_QuestionModel = value;
            }
        }

        TS_User_Entity m_author;

        protected TS_User_Entity Author {
            get {
                return m_author;
            }
            set {
                m_author = value;
            }
        }


        TS_School_Entity m_school;
        protected TS_School_Entity School {
            get {
                return m_school;
            }
            set {
                m_school = value;
            }
        }

        Guid QueryPreviewId {

            get {
                Guid code = Guid.Empty;//new Guid("929D18A2-EAED-4F6C-A05B-1C87586D94CF");//
                if (Guid.TryParse(ZJFramework10.Utility.QueryStringManager.GetValue<string>("p", Guid.Empty.ToString()), out code)) {
                    return code;
                }
                return code;
            }
        }

        protected void Page_Load(object sender, EventArgs e) {
            m_PreviewModel = TrainingsApp.Logic.TS_Preview_Logic.TS_Preview_View(QueryPreviewId);
            if (m_PreviewModel == null) {
                this.ShowError("前概念信息丢失");
                return;
            }
            m_QuestionModel = TrainingsApp.Logic.TS_Question_Logic.TS_Question_View(m_PreviewModel.QuestionId.Value);
            if (m_QuestionModel == null) {
                this.ShowError("前概念问题丢失");
                return;
            }
            m_author = TrainingsApp.Logic.TS_User_Logic.TS_User_View(m_PreviewModel.TeachId.Value);

            m_school = TrainingsApp.Logic.TS_School_Logic.TS_School_View(m_PreviewModel.SchoolId.Value);
            if (!IsPostBack) {
                DataBind();
            }
        }

        public void DataBind() {


            var allDataSource = TS_Preview_Result_Logic.TS_Preview_Result_ListByPreviewId(m_PreviewModel.PreviewId.Value);
            this.rptDataSource.DataSource = allDataSource;
            this.rptDataSource.DataBind();
            if (!allDataSource.Exists(u => u.GroupId == 99)) {
                btnGroupOk.Visible = false;
            }

        }

        protected void btnGroupOk_Click(object sender, EventArgs args) {

            var allDataSource = TS_Preview_Result_Logic.TS_Preview_Result_ListByPreviewId(m_PreviewModel.PreviewId.Value);
            var userIds = allDataSource.Where(u => u.GroupId == 99).Select(u => u.TS_Preview_Result_List.Select(t => t.UserId.Value)).FirstOrDefault().ToList();

            if (userIds != null) {
                int newGroupId = TS_Preview_Result_Logic.TS_Preview_Result_MaxGroupId(QueryPreviewId);


                List<TS_Preview_Result_Entity> updates = new List<TS_Preview_Result_Entity>();
                int i = 0;
                DateTime now = DateTime.Now;
                foreach (var userId in userIds) {
                    int tempId = userId.AsInt(0);
                    if (tempId > 0) {
                        updates.Add(new TS_Preview_Result_Entity {
                            Sort = i,
                            GroupId = newGroupId,
                            UserId = tempId,
                            PreviewId = QueryPreviewId,
                            GroupDate = now
                        });
                        i++;
                    }
                }

                if (updates.Count > 0) {
                    TS_Preview_Result_Logic.TS_Preview_Result_UpdateGroupId(updates);
                }

                DataBind();
            }
        }

        protected void btnOperation_Click(object sender, EventArgs args) {

            long upUser = ZJFramework10.Utility.FormManager.GetValue<long>("upTheUser", 0);
            if (upUser > 0) {
                TS_Preview_Result_Logic.TS_Preview_Result_UpdateSort(new TS_Preview_Result_Entity {
                    UserId = upUser,
                    PreviewId = QueryPreviewId
                });
                DataBind();
            }
        }


        protected void btnGroup_Click(object sender, EventArgs args) {

            string requestCheckbox = ZJFramework10.Utility.FormManager.GetValue<string>("checkbox");
            if (!string.IsNullOrEmpty(requestCheckbox)) {
                string[] userIds = requestCheckbox.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                int newGroupId = TS_Preview_Result_Logic.TS_Preview_Result_MaxGroupId(QueryPreviewId);


                List<TS_Preview_Result_Entity> updates = new List<TS_Preview_Result_Entity>();
                int i = 0;
                DateTime now = DateTime.Now;
                foreach (var userId in userIds) {
                    int tempId = userId.AsInt(0);
                    if (tempId > 0) {
                        updates.Add(new TS_Preview_Result_Entity {
                            Sort = i,
                            GroupId = newGroupId,
                            UserId = tempId,
                            PreviewId = QueryPreviewId,
                            GroupDate = now
                        });
                        i++;
                    }
                }

                if (updates.Count > 0) {
                    TS_Preview_Result_Logic.TS_Preview_Result_UpdateGroupId(updates);
                }

                DataBind();
            }
            //  Response.Write(ZJFramework10.Utility.FormManager.GetValue<string>("checkbox"));
        }

        protected void btnGroupUp_Click(object sender, EventArgs args) {

            long upUser = ZJFramework10.Utility.FormManager.GetValue<long>("upTheUser", 0);
            if (upUser > 0) {
                TS_Preview_Result_Logic.TS_Preview_Result_GroupUp(new TS_Preview_Result_Entity {
                    UserId = upUser,
                    PreviewId = QueryPreviewId
                });
                DataBind();
            }
        }

        protected string GetColor(int index, object groupId) {
            var gId = groupId.AsInt64(99);
            if (gId == 99) return "#FFFFFF";
            int left = (index + 1) % 7;
            switch (left) {
                case 1: return "#ffebe6";
                case 2: return "#ffffdd";
                case 3: return "#f2fae5";
                case 4: return "#dbd2f1";
                case 5: return "#faeecd";
                case 6: return "#e5f5cd";
                default: return "#e1f4ff";
            }
        }
    }
}